const path = require('path')
const { fileUploadError, unSupportedFileType, publishGoodsError, invalidGoodsID } = require('../constant/err.type')

const { createGoods, updtaeGoods, removeGoods, restoreGoods, findGoods } = require('../service/goods.service')

class GoodController {
    async upload(ctx, next) {
        // console.log(ctx.request.files);
        // let a = path.basename(ctx.request.files.file.filepath)
        // console.log('aaaaa', a);

        // ctx.body = '图片上传成功'

        // 完善类型判断
        const { file } = ctx.request.files
        // console.log(file);
        const fileTypes = ['image/png', 'image/jpeg', 'image/jpg'];
        if (file) {
            if (!fileTypes.includes(file.mimetype)) {
                return ctx.app.emit('error', unSupportedFileType, ctx)
            }
            ctx.body = {
                code: 0,
                message: '图片上传成功',
                result: {
                    good_img: path.basename(file.filepath),
                }
            }
        } else {
            return ctx.app.emit('error', fileUploadError, ctx)
        }
    }
    async create(ctx) {
        // 直接调用service的createGoods方法
        try {
            const { createdAt, updatedAt, ...res } = await createGoods(ctx.request.body);
            ctx.body = {
                code: 0,
                message: "发布商品成功",
                result: res
            }
        } catch (error) {
            console.log(error);
            return ctx.app.emit('error', publishGoodsError, ctx);
        }
    }

    async update(ctx) {
        // ctx.body = '修改商品成功'
        try {
            const res = await updtaeGoods(ctx.params.id, ctx.request.body)
            if (res) {
                ctx.body = {
                    code: 0,
                    message: '修改商品成功',
                    result: ''
                }
            } else {
                return ctx.app.emit('error', invalidGoodsID, ctx)
            }
        } catch (error) {
            console.error(err);
        }
    }
    async remove(ctx) {
        try {
            const res = await removeGoods(ctx.params.id)
            if (res) {
                ctx.body = {
                    code: 0,
                    message: '下架商品成功',
                    result: ""
                }
            } else {
                return ctx.app.emit('error', invalidGoodsID, ctx)
            }
        } catch (error) {
            console.error(err);
        }
        // await removeGoods

    }
    async restore(ctx) {
        try {
            const res = await restoreGoods(ctx.params.id)
            if (res) {
                ctx.body = {
                    code: 0,
                    message: '上架商品成功',
                    result: ""
                }
            } else {
                return ctx.app.emit('error', invalidGoodsID, ctx)
            }
        } catch (error) {
            console.error(err);
        }
    }
    async findAll(ctx) {
        //     解析pageNum和pageSize
        const { page = "1", pageSize = "10" } = ctx.request.query;
        const res = await findGoods(page, pageSize);
        ctx.body = {
            code: 0,
            massage: '获取商品列表成功',
            result: res
        }
    }
}

module.exports = new GoodController()